<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue';
import { ElMessage, FormInstance, FormRules } from 'element-plus';
import useStore from '@/store';
import { props } from './form';
defineOptions({ name: 'DictionaryForm' });
const _props = defineProps(props);
const save = async () => {
  if (!ruleFormRef.value) return;
  await ruleFormRef.value.validate((valid, fields) => {
    if (valid) {
      window.$api.dictionary.save(form).then(res => {
        ElMessage.success('保存成功!');
        useStore.window.windowRemove('DictionaryForm' + (form.id ?? ''));
      });
    }
  });
};
const ruleFormRef = ref<FormInstance>();
const form = reactive({
  id: null,
  dictionaryCategoryId: null,
  value: null,
  name: null,
  sort: null,
  remark: null,
});
const rules = reactive<FormRules>({
  value: [
    { required: true, message: '请输入字典值', trigger: 'blur' },
    { max: 50, message: '不能多于50个字符', trigger: 'blur' },
  ],
  name: [
    { required: true, message: '请输入字典名称', trigger: 'blur' },
    { max: 50, message: '不能多于50个字符', trigger: 'blur' },
  ],
});
const detail = () => {
  window.$api.dictionary.detail(_props.window.params.id).then(res => {
    form.dictionaryCategoryId = res.data.DictionaryCategoryId;
    form.id = res.data.Id;
    form.name = res.data.name;
    form.value = res.data.value;
    form.sort = res.data.sort;
    form.remark = res.data.remark;
  });
};
onMounted(() => {
  if (_props.window.params) {
    if (_props.window.params.id) {
      detail();
    }
    if (_props.window.params.dictionaryCategoryId) {
      form.dictionaryCategoryId = _props.window.params.dictionaryCategoryId;
    }
  }
});
</script>

<template>
  <windows-window :window="window">
    <template #button>
      <windows-button-group size="18">
        <windows-button icon="fluent-save-28-regular" @click="save"></windows-button>
      </windows-button-group>
    </template>
    <el-form ref="ruleFormRef" :model="form" :rules="rules" label-width="120px" class="demo-ruleForm" size="default" status-icon>
      <el-row>
        <el-col :span="24">
          <el-form-item label="字典名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入字典名称" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="字典值" prop="value">
            <el-input-number v-model="form.value" placeholder="请输入字典值" :controls="false" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="排序" prop="Sort">
            <el-input-number v-model="form.sort" :min="1" controls-position="right" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </windows-window>
</template>

<style scoped lang="scss">
#edit {
  height: 100%;
}
.el-form {
  margin: 30px 20px;
}
.custom-node {
  display: flex;
  align-items: center;
  span {
    margin-left: 5px;
  }
}
.select-icon {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  border: 1px solid #dcdfe6;
  border-radius: 5px;
  margin-top: 5px;
  background: #fff;
  width: 100%;
  height: 200px;
  overflow: auto;
  .el-image {
    margin: 10px;
    cursor: pointer;
    border-radius: 5px;
    padding: 5px;
    &:hover,
    &.active {
      background: #dcdfe6;
    }
  }
}
</style>
